package result

import (
	"github.com/gin-gonic/gin"
	"mcp-demo/internal/model/constant"
	"net/http"
	"time"
)

const (
	Success    = 200
	SuccessMsg = "success"
	Error      = 500
	ErrorMsg   = "网络开小差了!"
)

// Result 通用响应结构体
type Result[T any] struct {
	Success   bool   `json:"success"`   // 成功标志
	Code      int    `json:"code"`      // 状态码
	Message   string `json:"message"`   // 提示信息
	Timestamp int64  `json:"timestamp"` // 时间戳
	Data      T      `json:"data"`      // 响应数据
}

type NilResult = Result[interface{}]

// New 创建指针
func New[T any](success bool, code int, msg string, data T) *Result[T] {
	return &Result[T]{
		Success:   success,
		Code:      code,
		Message:   msg,
		Timestamp: time.Now().UnixMilli(),
		Data:      data,
	}
}

func Ok(c *gin.Context) {
	result := New[any](true, Success, SuccessMsg, nil)
	c.Set(constant.PayloadName, result)
	c.JSON(http.StatusOK, result)
}

func OkWithCodeMessage(code int, msg string, c *gin.Context) {
	result := New[any](true, code, msg, nil)
	c.Set(constant.PayloadName, result)
	c.JSON(http.StatusOK, result)
}

func OkWithMessage(message string, c *gin.Context) {
	result := New[any](true, Success, message, nil)
	c.Set(constant.PayloadName, result)
	c.JSON(http.StatusOK, result)
}

func OkWithData[T any](data T, c *gin.Context) {
	result := New[any](true, Success, SuccessMsg, data)
	c.Set(constant.PayloadName, result)
	c.JSON(http.StatusOK, result)
}

func Fail(c *gin.Context) {
	result := New[any](false, Error, ErrorMsg, nil)
	c.Set(constant.PayloadName, result)
	c.JSON(http.StatusOK, result)
}

func FailWithCodeMessage(code int, msg string, c *gin.Context) {
	result := New[any](false, code, msg, nil)
	c.Set(constant.PayloadName, result)
	c.JSON(http.StatusOK, result)
}

func FailWithMessage(message string, c *gin.Context) {
	result := New[any](false, Error, message, nil)
	c.Set(constant.PayloadName, result)
	c.JSON(http.StatusOK, result)
}

func OkResult[T any](data T) *Result[T] {
	result := New[T](true, Success, SuccessMsg, data)
	return result
}

func FailResult() *Result[any] {
	result := New[any](false, Error, ErrorMsg, nil)
	return result
}
